<?php
/*
	PookiWiki - A wiki unlike the rest.
	Copyright (C) 2012 Jordan Craw

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

	class PookiThemeInstance
	{
		public $theme_html;
	
		public $POOKI_DEFAULT_TITLE;
		public $POOKI_DEFAULT_NAME;
		public $POOKI_NAVIGATION;
		public $POOKI_LOGIN;
		public $POOKI_ARTICLE_ACTIONS;
		public $POOKI_ARTICLE_EDITING_ITEMS;
		public $POOKI_ARTICLE_CONTENT;
		public $POOKI_RELATED_CONTENT;
		public $POOKI_FOOTER_CONTENT;
	}
	
	class PookiThemeHandler
	{
		protected $theme_html;
		protected $available_sections = array();
		protected $instance;
		
		protected $defaults = array(
			"POOKI_DEFAULT_TITLE" => "A PookiWiki Wiki",
			"POOKI_DEFAULT_NAME" => "A PookiWiki Wiki"
		);
		
		
		protected function outputLines($lines)
		{
			return implode("\n",$lines);
		}
		
		
		protected function parseAvailableSections()
		{
			if(strpos($this->theme_html,"\n"))
			{
				$lines = explode("\n",$this->theme_html);
				
				if(count($lines))
				{
					$num_lines = count($lines);
					for($i = 0; $i < $num_lines; $i++)
					{
						if(preg_match("#{SUB:([A-Za-z_]+)}#",$lines[$i],$match))
						{
							$this->available_sections[$i] = $match[1];
							
							$replace = "";
							
							if(isset($this->instance->$match[1]))
							{
								$replace = $this->instance->$match[1];
							} 
							else
							{
								if(isset($this->defaults[$match[1]]))
								{
									$replace = $this->defaults[$match[1]];
								}
							}
							$new_value = str_replace("{SUB:" . $match[1] . "}",$replace,$lines[$i]);
							$lines[$i] = $new_value;
						}
					}
					return $this->outputLines($lines);
					
				}
				else
				{
					// throw new PookiError
				}
			}
		}

		public function __construct(PookiThemeInstance $instance)
		{
			$this->theme_html = $instance->theme_html;
			$this->instance = $instance;
			echo $this->parseAvailableSections();
		}

	}
	
	class PookiTheme
	{
		protected $theme_contents;
		protected $init_object;
		
		protected $theme_elements = array();
		protected $theme_output;
		
		protected function outputTheme()
		{
			if(in_array("default.html",$this->theme_contents))
			{
				$default_theme = $this->init_object->selected_theme;
				$reader = new PookiFileReader(new PookiFile("themes/" . $default_theme . "/default.html"));
				return $reader;
			}
		}
	
		public function __construct(PookiThemeInstance $instance)
		{
			$this->init_object = new PookiThemeInitiator();
			$this->theme_contents = $this->init_object->outputArray();
			$this->theme_output = $this->outputTheme();
			
			$instance->theme_html = $this->theme_output;
			$themeparser = new PookiThemeHandler($instance);
		}
	
	}

	class PookiThemeItem
	{
		public $name;
		public $default_folder;
		public $valid;
	}
	
	
	class PookiThemeInitiator
	{
		public $theme_mode;
		public $theme_folder_contents = FALSE;
		public $selected_theme;
		
		protected $can_theme;
	
		protected $file_instance;
		public $existing_themes = array();
		
		protected $images_folder_intact;
		protected $default_folder_intact;
		protected $default_folder_themable;
		
		protected function checkThemeExists($theme_name)
		{
			return isset($this->existing_themes[$theme_name]);
		}
		
		public function getTheme($theme_name)
		{
			if($this->checkThemeExists($theme_name))
			{
				return $this->existing_themes[$theme_name];
			}
		}
		
		protected function checkExistingThemeFolders()
		{
			$file_reader = new PookiFileReader($this->file_instance);
			$folders = $file_reader->outputArray();
			$remove_items = array();
			
			if(in_array("images",$folders))
			{
				$this->images_folder_intact = TRUE;
				$remove_items[] = "items";
			}
			if(in_array("default",$folders))
			{
				$this->default_folder_intact = TRUE;
				$remove_items[] = "default";
			}
			if(!in_array("default",$folders))
			{
				$this->theme_mode = POOKIWIKI_CONST_THEME_MODE_FAILSAFE;
			}
			
			if(count($folders) > count($remove_items))
			{
				$additional_themes = array_diff($remove_items,$folders);
				$this->existing_themes = $additional_themes;
				$this->can_theme = TRUE;
				
				// TODO: go through each additional theme and add a "skeleton" theme 
			}
			elseif($this->default_folder_intact)
			{
				$theme = new PookiThemeItem();
				$theme->name = "default"; // TODO: Update
				$this->default_folder = "default";
				$this->existing_themes["default"] = $theme;
				$this->can_theme = TRUE;
				
				$this->selected_theme = "default";
			}
			else
			{
				// throw new error
			}
		}
	
		protected function checkThemeFolderContents(PookiFile $file)
		{
			$reader = new PookiFileReader($file);
			return $reader->outputArray();
		}
		
		public function outputArray()
		{
			return $this->theme_folder_contents;
		}
		
		public function __construct($use_theme = "default")
		{
			$this->file_instance = new PookiFile("themes");
			$this->checkExistingThemeFolders();
			
			if($this->can_theme)
			{
				$theme = $this->getTheme($use_theme);
				unset($this->file_instance);
				
				$this->theme_folder_contents = $this->checkThemeFolderContents(new PookiFile("themes/" . $use_theme));
			}
			
			
		}
	
	
	
	
	}



?>